package vn.edu.saigontech.ncms.web.offering;

import java.util.Collection;
import java.util.Date;

import vn.edu.saigontech.ncms.model.enumeration.TeachingInterruptionReason;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.TeachingInterruption;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateTeachingInterruptionAction extends AbstractValidator {
	
	private Long id;
	private Long offeringId;
	private Long teacherId;
	private Long staffId;
	private String fromDate;
	private String toDate;
	private String reason;
	private Long substituteTeacherId;
	private String makeupFromDate;
	private String makeupToDate;
	
	public String check() {
		if(Checker.isNull(this.offeringId)) {
			this.sendKeyError("validator.required", "class");
		}
		if(Checker.isNull(this.teacherId)) {
			this.sendKeyError("validator.required", "teacher");
		}
		if(Checker.isEmpty(this.fromDate)) {
			this.sendKeyError("validator.required", "fromDate");
		}
		if(!Checker.isDate(this.fromDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "fromDate", "format.date");
		}
		if(Checker.isEmpty(this.toDate)) {
			this.sendKeyError("validator.required", "toDate");
		}
		if(!Checker.isDate(this.toDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "toDate", "format.date");
		}
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		if(!DateUtil.isBetween(fromDateDate, offering.getStartDate(), offering.getEndDate())) {
			String f = DateUtil.toString(offering.getStartDate(), this.i18n("format.date"));
			String t = DateUtil.toString(offering.getEndDate(), this.i18n("format.date"));
			this.sendKeyError("validator.between", "fromDate", f, t);
		}
		if(!DateUtil.isBetween(toDateDate, offering.getStartDate(), offering.getEndDate())) {
			String f = DateUtil.toString(offering.getStartDate(), this.i18n("format.date"));
			String t = DateUtil.toString(offering.getEndDate(), this.i18n("format.date"));
			this.sendKeyError("validator.between", "toDate", f, t);
		}
		if(fromDateDate.after(toDateDate)) {
			this.sendKeyError("validator.greater", "toDate", "fromDate");
		}
		Teacher teacher = TeacherService.INSTANCE.findById(this.teacherId);		
		
		if(TeachingInterruptionReason.SUBSTITUTE.equals(TeachingInterruptionReason.create(this.reason))) {
			if(Checker.isNull(this.substituteTeacherId)) {
				this.sendKeyError("validator.required", "substituteTeacher");
			}
			Teacher substituteTeacher = TeacherService.INSTANCE.findById(this.substituteTeacherId);
			if(teacher.getId().equals(substituteTeacher.getId())) {
				this.sendKeyError("validator.invalid", "substituteTeacher");
			}
			if(!teacher.getTeacherType().equals(substituteTeacher.getTeacherType())) {
				this.sendKeyError("validator.invalid", "substituteTeacher");
			}
		}
		else if(TeachingInterruptionReason.CANCEL.equals(TeachingInterruptionReason.create(this.reason))) {
			if(!Checker.isEmpty(this.makeupFromDate) && !Checker.isEmpty(this.makeupToDate)) {
				Date makeupFromDateDate = DateUtil.fromString(this.makeupFromDate, this.i18n("format.date"));
				Date makeupToDateDate = DateUtil.fromString(this.makeupToDate, this.i18n("format.date"));
				
				String f = DateUtil.toString(offering.getStartDate(), this.i18n("format.date"));
				String t = DateUtil.toString(offering.getEndDate(), this.i18n("format.date"));
				
				if(!DateUtil.isBetween(makeupFromDateDate, offering.getStartDate(), offering.getEndDate())) {
					this.sendKeyError("validator.between", "makeupDate", f, t);
				}
				if(!DateUtil.isBetween(makeupToDateDate, offering.getStartDate(), offering.getEndDate())) {
					this.sendKeyError("validator.between", "makeupDate", f, t);
				}
				if(makeupFromDateDate.after(makeupToDateDate)) {
					this.sendKeyError("validator.invalid", "makeupDate");
				}
			}
			else if(!Checker.isEmpty(this.makeupFromDate) || !Checker.isEmpty(this.makeupFromDate)) {
				this.sendKeyError("validator.required", "makeupDate");
			}
		}
		else {
			if(Checker.isNull(this.staffId)) {
				this.sendKeyError("validator.required", "staff");
			}
		}
		Collection<Date> dateRange = DateUtil.sequence(fromDateDate, toDateDate);
		if(Checker.isNull(this.id)) {
			for(TeachingInterruption each : offering.getTeachingInterruptions()) {
				if(teacher.getId().equals(each.getTeacher().getId())) {
					Collection<Date> r = DateUtil.sequence(each.getFromDate(), each.getToDate());
					r.retainAll(dateRange);
					if(!r.isEmpty()) {
						this.sendError("Date range has already been used");
						break;
					}
				}
			}
		}
		else {
			for(TeachingInterruption each : offering.getTeachingInterruptions()) {
				if(teacher.getId().equals(each.getTeacher().getId())) {
					if(!each.getId().equals(this.id)) {
						Collection<Date> r = DateUtil.sequence(each.getFromDate(), each.getToDate());
						r.retainAll(dateRange);
						if(!r.isEmpty()) {
							this.sendError("Date range has already been used");
							break;
						}
					}
				}
			}
		}
		return this.toSuccess();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public Long getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(Long teacherId) {
		this.teacherId = teacherId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public Long getSubstituteTeacherId() {
		return substituteTeacherId;
	}

	public void setSubstituteTeacherId(Long substituteTeacherId) {
		this.substituteTeacherId = substituteTeacherId;
	}

	public Long getStaffId() {
		return staffId;
	}

	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}

	public String getMakeupFromDate() {
		return makeupFromDate;
	}

	public void setMakeupFromDate(String makeupFromDate) {
		this.makeupFromDate = makeupFromDate;
	}

	public String getMakeupToDate() {
		return makeupToDate;
	}

	public void setMakeupToDate(String makeupToDate) {
		this.makeupToDate = makeupToDate;
	}
}
